package com.ruoyi.basic.service;

import lombok.RequiredArgsConstructor;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.basic.mapper.JySemesterMapper;
import com.ruoyi.basic.domain.JySemester;

/**
 * 学期Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-05
 */
@Service
@RequiredArgsConstructor
public class JySemesterService {
    private final JySemesterMapper jySemesterMapper;

    /**
     * 查询学期
     *
     * @param id 学期主键
     * @return 学期
     */
    public JySemester selectJySemesterById(Long id) {
        return jySemesterMapper.selectJySemesterById(id);
    }

    /**
     * 查询学期列表
     *
     * @param jySemester 学期
     * @return 学期
     */
    public List<JySemester> selectJySemesterList(JySemester jySemester) {
        return jySemesterMapper.selectJySemesterList(jySemester);
    }

    /**
     * 新增学期
     *
     * @param jySemester 学期
     * @return 结果
     */
    public int insertJySemester(JySemester jySemester) {
        jySemester.setCreateTime(DateUtils.getNowDate());
        int result = jySemesterMapper.insertJySemester(jySemester);
        if (result > 0) {
            this.updateSemesterCurrent(jySemester.getId(), (byte) 1);
        }
        return result;
    }

    /**
     * 修改学期
     *
     * @param jySemester 学期
     * @return 结果
     */
    public int updateJySemester(JySemester jySemester) {
        jySemester.setUpdateTime(DateUtils.getNowDate());
        return jySemesterMapper.updateJySemester(jySemester);
    }

    /**
     * 批量删除学期
     *
     * @param ids 需要删除的学期主键
     * @return 结果
     */
    public int deleteJySemesterByIds(Long[] ids) {
        return jySemesterMapper.deleteJySemesterByIds(ids);
    }
    /**
     * 删除学期信息
     *
     * @param id 学期主键
     * @return 结果
     */
    public int updateSemesterCurrent(Long id, Byte current) {
        if (null != current && 1 == current) {
            jySemesterMapper.cancelCurrent(null);
            return jySemesterMapper.setCurrent(id);
        } else {
            return jySemesterMapper.cancelCurrent(id);
        }
    }

    /**
     * 删除学期信息
     *
     * @param id 学期主键
     * @return 结果
     */
    public int deleteJySemesterById(Long id) {
        return jySemesterMapper.deleteJySemesterById(id);
    }
}
